package com.situ.mall.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.mall.admin.entity.Menu;
import com.situ.mall.admin.exception.BusinessException;
import com.situ.mall.admin.mapper.MenuMapper;
import com.situ.mall.admin.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.admin.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author membergpf
 * @since 2023-03-18
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public LayuiTableVO doList() {
        LayuiTableVO vo = new LayuiTableVO();
        vo.setData(baseMapper.selectList(null));
        return vo;
    }

    @Override
    public void doDelete(Integer id) {
        List<Menu> list =baseMapper.selectByParentId(id);
        if(list.size() > 0){

            throw new BusinessException("存在子菜单，不可以删除");
        }else{
            baseMapper.deleteById(id);
        }}

    @Override
    public InitVO init() {
       InitVO vo= new InitVO();
        HomeInfoVO homeInfoVO =new HomeInfoVO();
        homeInfoVO.setTitle("首页");
        homeInfoVO.setHref("");
        vo.setHomeInfo(homeInfoVO);

        Map<String ,String > lognMap =new HashMap<>();
        lognMap.put("title","小米商城后台");
        lognMap.put("image","images/logo.png");
        lognMap.put("href","");
       vo.setLogoInfo(lognMap);
       //有几个一级菜单就返回几条数据
//       List<MenuVO>list =new ArrayList<>();
//
//       //查询所有的一级菜单
//       List<Menu> tops =menuMapper.subs(0);
//        for(Menu m:tops){
//            MenuVO menuVO = new MenuVO(m.getTitle(), m.getHref(), m.getIcon(), m.getTarget());
////            menuVO.setTitle(m.getTitle());
////            menuVO.setIcon(m.getIcon());
////            menuVO.setHref(m.getHref());
////            menuVO.setTarget(m.getTarget());
//            //查当前这个m菜单对应的二级菜单
//            List<Menu>subs =menuMapper.subs(m.getId());
//            menuVO.setChild(new ArrayList<>());
//            for (Menu sub: subs)  {
//                MenuVO mvo = new MenuVO(sub.getTitle(), sub.getHref(), sub.getIcon(), sub.getTarget());
////                mvo.setTitle(sub.getTitle());
////                mvo.setHref(sub.getHref());
////                mvo.setIcon(sub.getIcon());
////                mvo.setTarget(sub.getTarget());
//                menuVO.getChild().add(mvo);
//            }
//
//            list.add(menuVO);
//        }
//       vo.setMenuInfo(list);


        //Mabites里的高级映射，可以直接返回vo套vo的格式
        vo.setMenuInfo(menuMapper.menuInfo());
       return vo;
    }

}
